﻿
@{
	ViewBag.Title = "GetNeedMaintain";

}


<link rel="stylesheet" href="../../Customiz/layui/css/layui.css" media="all">
<link rel="stylesheet" href="../../Customiz/css/admin.css" media="all">

<div class="layui-fluid">
	<div class="layui-form layui-card-header layuiadmin-card-header-auto">
		<div class="layui-form-item">
			<div class="layui-inline">
				<label class="layui-form-label">军械名称</label>
				<div class="layui-input-block">
					<input type="text" name="Report_ArmyName" placeholder="请输入" autocomplete="off" class="layui-input">
				</div>
			</div>
			<div class="layui-inline">
				<label class="layui-form-label">所属仓库</label>
				<div class="layui-input-block">
					<select name="Warehouse" id="Warehouse">
						<option value=""></option>
					</select>
				</div>
			</div>	
			<div class="layui-inline">
				<button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="Ord-Report-back-search">
					<i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
				</button>
			</div>
		</div>
	</div>
	<div class="layui-card">
		<div class="layui-card-body">
			<div style="padding-bottom: 10px;">
				<button id="maintain" class="layui-btn layuiadmin-btn-admin" data-type="maintain">维修</button>
				<button id="finish"  class="layui-btn layuiadmin-btn-admin" data-type="finish">回库</button>
			</div>

			<table id="Ord_Maintain_Table" lay-filter="Ord_Maintain_Table"></table>

		</div>
	</div>
</div>

<script src="../../Customiz/layui/layui.js"></script>
<script>

    layui.use(['table'], function () {
		var table = layui.table;
		var form = layui.form;
		var $ = layui.$;
		var layer = layui.layer;
		var userid = ''; 
		if (@ViewBag.RoleID != "0" && @ViewBag.RoleID != "5")
		{
		    //禁止按钮
		    $('#maintain').addClass('layui-btn-disabled').attr('disabled', "true");
		    $('#finish').addClass('layui-btn-disabled').attr('disabled', "true");
		}
		if (@ViewBag.RoleID == "5") {
			userid = "@ViewBag.UserID";
		}
		$.ajax({
            url: '/Index/GetWarehouseData',
            dataType: 'json',
            type: 'Post',
            success: function (data) {
                $.each(data.data, function (index, item) {
                    $('#Warehouse').append(new Option(item.Warehouse_Name, item.Warehouse_ID)); // 下拉菜单里添加元素
                });
                layui.form.render("select");
                //重新渲染 固定写法
            }
        })
        //用户管理
        table.render({
			elem: '#Ord_Maintain_Table',
			url: '/Maintain/GetMaintainData?UserID=' + userid, //模拟接口	
            page: true,
            cols: [
                [{
                    type: 'checkbox',
                    fixed: 'left'
                }, {
                    field: 'Report_ArmyName',
                    title: '军械名称'
				}, {
					field: 'Report_Batch',
					title: '军械批次'
				}, {
					field: 'Report_WarehouseName',
					title: '仓库名称'
				},  {
                    field: 'Report_Num',
                    title: '军械数量'
				}, {
					field: 'Report_BorrowNum',
					title: '借出数量',
				}, {
                    field: 'Report_MaintainTime',
                    title: '维修时间'
				}, {
					field: 'Report_RepairCycle',
					title: '维修周期',
				}, {
					field: 'Report_MaintainStatus',
					title: '维修状态',
					templet: function (data) {
						if (data.Report_MaintainStatus == 0) { return "维修中"; }
						if (data.Report_MaintainStatus == 1) { return "正常"; }
						}

                }
                ]
            ],
            text: '对不起，加载出现异常！'
        });

        //监听查询
		//监听搜索
		form.on('submit(Ord-Report-back-search)', function (data) {
			var field = data.field;

			//执行重载
			table.reload('Ord_Maintain_Table', {
				where: field
			});
		});

        //事件
        var active = {
            maintain: function () {
                var checkStatus = table.checkStatus('Ord_Maintain_Table'),
                    checkData = checkStatus.data; //得到选中的数据

                if (checkData.length < 1) {
                    return layer.msg('请至少选择一条数据');
                }
                $.each(checkData, function (i, item) {
                    if (item.Report_BorrowNum != 0 )
                    {
                        return layer.msg('请归还后统一维修');
                    }
				});
				$.each(checkData, function (i, item) {
					if (item.Report_MaintainStatus == 0) {
						return layer.msg('装备已在维修中');
					}
				});
                layer.confirm('确定维修吗？', function (index) {
                    //循环取所有id
                    var maintainS = new Array();
                    $.each(checkData, function (i, item) {
                        maintainS.push(item.Report_ID);
                    });
                    if (maintainS.length > 0) {
                        $.ajax({
                            type: "Post",
                            url: '/Maintain/MaintainReport', //配置维护功能
                            dataType: 'json',
                            data: {
                                IDs: maintainS
                            },
                            success: function (data) {
                                if (data == true) {
                                    layer.msg('维修进行中');
                                    table.reload('Ord_Maintain_Table');
                                } else {
                                    layer.msg('维修失败');
                                }

                            }
                        });
                    }
                });
            },           
            finish: function () {
                var checkStatus = table.checkStatus('Ord_Maintain_Table'),
                    checkData = checkStatus.data; //得到选中的数据

				if (checkData.length > 1 || checkData==0) {
                    return layer.msg('请选择一条数据');
                }
                $.each(checkData, function (i, item) {
                    if (item.Report_ReportStatus != 1) {
                        return layer.msg('只允许确认已在维修的数据');
                    }
                });
                layer.confirm('确认提交吗？', function (index) {
                    //循环取所有id
					var finishS = new Array();
					$.each(checkData, function (i, item) {
						finishS.push(item.Report_ID);
					});
                   // $.each(checkData, function (i, item) {
                    //    finishS = item.Report_ID;
                   // });
                    if (finishS.length > 0) {
                        $.ajax({
                            type: "Post",
                            url: '/Maintain/FinishReport', //配置回库功能
                            dataType: 'json',
                            data: {
                                IDs: finishS,
                                //state:1
                            },
                            success: function (data) {
                                if (data == true) {
                                    layer.msg('已回库');
									table.reload('Ord_Maintain_Table');
                                } else {
                                    layer.msg('回库失败');
                                }

                            }
						});
						$.ajax({
							type: "Post",
							url: '/Maintain/FinishRecord', //配置回库记录
							dataType: 'json',
							data: {
								
								Maintain_Name: checkData[0].Report_ArmyName,
								Maintain_Batch: checkData[0].Report_Batch,
								Maintain_WarehouseID: checkData[0].Report_WarehouseID,
								Maintain_WarehouseName: checkData[0].Report_WarehouseName,
								Maintain_Num: checkData[0].Report_Num,
								
							},
							success: function (data) {
								if (data == true) {
									//layer.msg('已回库');
									//table.reload('Ord_Maintain_Table');
								} else {
									//layer.msg('回库失败');
								}

							}
						});
                    }
                });
            }

           
        }
        //监测点击，根据值确认执行哪个操作
        $('.layui-btn.layuiadmin-btn-admin').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

    });
</script>

